'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 合成道具
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var rewardType = require('../../../../consts/rewardType');
var equipConf = require('../../../../config/equipConf');
var rewardService = require('../../../../services/rewardService');
var roleService = require('../../../../services/roleService');
var composeService = require('../../../../services/composeService');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var property_type = msg.property_type;

    if (!property_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    var success = 0;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['bag', 'role', 'accrued'], cb);
            },
            function (cb) {
                var bag_model = player_manager.bag;
                var role_model = player_manager.role;

                var property_compose_conf = dataApi.property_compose.findById(property_type);
                if (!property_compose_conf) {
                    return next(null, {
                        code: Code.BAG_PROPERTY_CAN_NOT_COMPOSE
                    });
                }

                // 扣除金币
                if (!roleService.subGold(player_manager, property_compose_conf.cost_gold)) {
                    return next(null, {
                        code: Code.USER_GOLD_NOT_ENOUGH
                    });
                }

                for (var i = property_compose_conf.cost_num - 1; i >= 0; i--) {
                    if (!bag_model.subProperty(property_type)) {
                        return next(null, {
                            code: Code.BAG_PROPERTY_NOT_ENOUGH
                        });
                    }
                }

                success = _.random(0, 100) < property_compose_conf.probability;
                if (success) {
                    bag_model.addProperty(property_compose_conf.compose_id, 1);
                    reward_list.push({
                        reward: property_compose_conf.compose_id,
                        reward_num: 1,
                        reward_type: rewardType.PROPERTY,
                        reward_value: 0,
                        hole_num: 0
                    });
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    success: success,
                    reward_list: reward_list
                }
            });
        }
    );
};
